// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.actions;

import java.util.ArrayList;
import java.util.List;

import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.core.warehouse.IWarehouseView;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-10 qiang.zhang $
 * 
 */
public abstract class AbstractAction extends Action {

    protected List<IWarehouseNode> pnodes;

    protected IWarehouseView view;

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractAction constructor comment.
     */
    public AbstractAction() {
        super();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractAction constructor comment.
     * 
     * @param name
     */
    public AbstractAction(String name) {
        super(name);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getSelection".
     * 
     * @return
     */
    protected final ISelection getSelection() {
        IWorkbenchPartSite site = getActivePage().getActivePart().getSite();
        if (site == null) {
            return null;
        }
        return site.getSelectionProvider().getSelection();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getActivePage".
     * 
     * @return
     */
    protected final IWorkbenchPage getActivePage() {
        return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setEnable".
     */
    public void setEnable() {
        ISelection selection = getSelection();
        if (selection == null) {
            setEnabled(false);
        }
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection selection2 = (IStructuredSelection) selection;
            if (selection2.isEmpty()) {
                setEnabled(false);
            } else {
                pnodes = new ArrayList<IWarehouseNode>();
                for (Object e : selection2.toList()) {
                    pnodes.add((IWarehouseNode) e);
                }
                setEnabled(true);
            }
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refresh".
     * 
     * @param node
     */
    public void refresh(Object node) {
        refresh();
        getWarehouseView().refresh(node);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refresh".
     * 
     * @param node
     */
    public void refresh() {
        getWarehouseView().refresh();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getWarehouseView".
     * 
     * @return
     */
    protected IWarehouseView getWarehouseView() {
        if (view == null) {
            IViewPart findView = getActivePage().findView(IWarehouseView.VIEW_ID);
            view = (IWarehouseView) findView;
        }
        return view;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getPath".
     * 
     * @return
     */
    protected IPath getPath() {
        return pnodes.get(0).getWarehousePath();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getPath".
     * 
     * @param node
     * @return
     */
    protected IPath getPath(IWarehouseNode node) {
        return node.getWarehousePath();
    }

}
